<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class GamesController extends AppController {

    var $layout = 'default';
    var $uses = array(
        'Game', 'Comment'
    );
    var $cacheAction = array(
        'index' => 120
    );
    var $components = array(
        'Security', 'Session'
    );
    var $scripts = array('jquery-1.7.2.min.js', 'custom_v1.24.js');

    function service_builtopcomment() {
        $this->Comment->top_comment();
        exit;
    }

    function index() {
        $this->set('title_for_layout', 'danh sách game');

        $page = array_key_exists('page', $this->params['named']) ? $this->params['named']['page'] : 1;

        $list_ids = array();
        $conditions = array(
            'Game.published' => 1
        );
        $this->paginate = array(
            'fields' => array('Game.id, Game.hot_icon, Game.alias, Game.title, Game.thumbnail, Game.folder_url, Game.play_times'),
            'conditions' => $conditions,
            'limit' => 24,
            'order' => array(
                'Game.id' => 'DESC'
            ),
        );

        $data = $this->paginate('Game');
        shuffle($data);

        $this->set('total', $this->params['paging']['Game']['count']);
        $this->set('data', $data);
        $this->set('thumbnail_thumb_url_path', Configure::read('game_flash_thumbnail_url_path'));
        $this->set('start_page', $page);
    }

    function game2d() {
        $this->set('title_for_layout', 'Game 2D');

        $page = array_key_exists('page', $this->params['named']) ? $this->params['named']['page'] : 1;

        $list_ids = array();
        $conditions = array(
            'Game.published' => 1,
            'Game.group_id' => 1
        );
        $this->paginate = array(
            'fields' => array('Game.id, Game.hot_icon, Game.alias, Game.title, Game.thumbnail, Game.folder_url, Game.play_times'),
            'conditions' => $conditions,
            'limit' => 24,
            'order' => array(
                'Game.id' => 'DESC'
            ),
        );

        $data = $this->paginate('Game');

        shuffle($data);
        $this->set('description_for_layout', "sưu tầm hàng ngàn game flash mini 2d thuộc nhiều thể loại.");

        $this->set('total', $this->params['paging']['Game']['count']);
        $this->set('data', $data);
        $this->set('thumbnail_thumb_url_path', Configure::read('game_flash_thumbnail_url_path'));
        $this->set('start_page', $page);
        $this->set('game2d_css', ' class="active"');
    }

    function game2d_lastest() {
        $this->set('title_for_layout', 'Game 2D mới nhất');

        $page = array_key_exists('page', $this->params['named']) ? $this->params['named']['page'] : 1;

        $list_ids = array();
        $conditions = array(
            'Game.published' => 1,
            'Game.group_id' => 1
        );
        $this->paginate = array(
            'fields' => array('Game.id, Game.hot_icon, Game.alias, Game.title, Game.thumbnail, Game.folder_url, Game.play_times'),
            'conditions' => $conditions,
            'limit' => 24,
            'order' => array(
                'Game.ordering' => 'ASC'
            ),
        );

        $data = $this->paginate('Game');
        $this->set('description_for_layout', "sưu tầm hàng ngàn game flash mini 2d mới nhất thuộc nhiều thể loại.");

        $this->set('total', $this->params['paging']['Game']['count']);
        $this->set('data', $data);
        $this->set('thumbnail_thumb_url_path', Configure::read('game_flash_thumbnail_url_path'));
        $this->set('start_page', $page);
        $this->set('game2d_css', ' class="active"');
    }

    function game2d_hottest() {
        $this->set('title_for_layout', 'Game 2D hay nhất');

        $page = array_key_exists('page', $this->params['named']) ? $this->params['named']['page'] : 1;

        $list_ids = array();
        $conditions = array(
            'Game.published' => 1,
            'Game.group_id' => 1,
            'Game.hot_game_incat' => 1
        );
        $this->paginate = array(
            'fields' => array('Game.id, Game.hot_icon, Game.alias, Game.title, Game.thumbnail, Game.folder_url, Game.play_times'),
            'conditions' => $conditions,
            'limit' => 24,
            'order' => array(
                'Game.hot_game_incat_ordering' => 'ASC'
            ),
        );

        $data = $this->paginate('Game');
        $this->set('description_for_layout', "sưu tầm hàng ngàn game flash mini 2d hay nhất thuộc nhiều thể loại.");

        $this->set('total', $this->params['paging']['Game']['count']);
        $this->set('data', $data);
        $this->set('thumbnail_thumb_url_path', Configure::read('game_flash_thumbnail_url_path'));
        $this->set('start_page', $page);
        $this->set('game2d_css', ' class="active"');
    }

    function game2d_popular() {
        $this->set('title_for_layout', 'Game 2D nhiều người chơi nhất');

        $page = array_key_exists('page', $this->params['named']) ? $this->params['named']['page'] : 1;

        $list_ids = array();
        $conditions = array(
            'Game.published' => 1,
            'Game.group_id' => 1,
            'Game.popular_incat' => 1
        );
        $this->paginate = array(
            'fields' => array('Game.id, Game.alias, Game.hot_icon, Game.title, Game.thumbnail, Game.folder_url, Game.play_times'),
            'conditions' => $conditions,
            'limit' => 24,
            'order' => array(
                'Game.popular_incat_ordering' => 'ASC'
            ),
        );

        $data = $this->paginate('Game');
        $this->set('description_for_layout', "sưu tầm hàng ngàn game flash mini 2d nhiều người chơi nhất.");

        $this->set('total', $this->params['paging']['Game']['count']);
        $this->set('data', $data);
        $this->set('thumbnail_thumb_url_path', Configure::read('game_flash_thumbnail_url_path'));
        $this->set('start_page', $page);
        $this->set('game2d_css', ' class="active"');
    }

    function game3d() {
        $this->set('title_for_layout', 'Game 3D');

        $page = array_key_exists('page', $this->params['named']) ? $this->params['named']['page'] : 1;

        $list_ids = array();
        $conditions = array(
            'Game.published' => 1,
            'Game.group_id' => 2
        );
        $this->paginate = array(
            'fields' => array('Game.id, Game.alias, Game.hot_icon, Game.title, Game.thumbnail, Game.folder_url, Game.play_times'),
            'conditions' => $conditions,
            'limit' => 24,
            'order' => array(
                'Game.id' => 'DESC'
            ),
        );
        $data = $this->paginate('Game');
        shuffle($data);
        $this->set('description_for_layout', "sưu tầm hàng ngàn game flash mini 3d thuộc nhiều thể loại.");

        $this->set('total', $this->params['paging']['Game']['count']);
        $this->set('data', $data);
        $this->set('thumbnail_thumb_url_path', Configure::read('game_flash_thumbnail_url_path'));
        $this->set('start_page', $page);
        $this->set('game3d_css', ' class="active"');
    }

    function game3d_lastest() {
        $this->set('title_for_layout', 'Game 3D mới nhất');

        $page = array_key_exists('page', $this->params['named']) ? $this->params['named']['page'] : 1;

        $list_ids = array();
        $conditions = array(
            'Game.published' => 1,
            'Game.group_id' => 2
        );
        $this->paginate = array(
            'fields' => array('Game.id, Game.alias, Game.hot_icon, Game.title, Game.thumbnail, Game.folder_url, Game.play_times'),
            'conditions' => $conditions,
            'limit' => 24,
            'order' => array(
                'Game.ordering' => 'ASC'
            ),
        );

        $data = $this->paginate('Game');
        $this->set('description_for_layout', "sưu tầm hàng ngàn game flash mini 3d mới nhất.");

        $this->set('total', $this->params['paging']['Game']['count']);
        $this->set('data', $data);
        $this->set('thumbnail_thumb_url_path', Configure::read('game_flash_thumbnail_url_path'));
        $this->set('start_page', $page);
        $this->set('game3d_css', ' class="active"');
    }

    function game3d_hottest() {
        $this->set('title_for_layout', 'Game 3D hay nhất');

        $page = array_key_exists('page', $this->params['named']) ? $this->params['named']['page'] : 1;

        $list_ids = array();
        $conditions = array(
            'Game.published' => 1,
            'Game.group_id' => 2,
            'Game.hot_game_incat' => 1
        );
        $this->paginate = array(
            'fields' => array('Game.id, Game.alias, Game.hot_icon, Game.title, Game.thumbnail, Game.folder_url, Game.play_times'),
            'conditions' => $conditions,
            'limit' => 24,
            'order' => array(
                'Game.hot_game_incat_ordering' => 'ASC'
            ),
        );

        $data = $this->paginate('Game');
        $this->set('description_for_layout', "sưu tầm hàng ngàn game flash mini 3d hay nhất.");

        $this->set('total', $this->params['paging']['Game']['count']);
        $this->set('data', $data);
        $this->set('thumbnail_thumb_url_path', Configure::read('game_flash_thumbnail_url_path'));
        $this->set('start_page', $page);
        $this->set('game3d_css', ' class="active"');
    }

    function game3d_popular() {
        $this->set('title_for_layout', 'Game 3D nhiều người chơi nhất');

        $page = array_key_exists('page', $this->params['named']) ? $this->params['named']['page'] : 1;

        $list_ids = array();
        $conditions = array(
            'Game.published' => 1,
            'Game.group_id' => 2,
            'Game.popular_incat' => 1
        );
        $this->paginate = array(
            'fields' => array('Game.id, Game.alias, Game.hot_icon, Game.title, Game.thumbnail, Game.folder_url, Game.play_times'),
            'conditions' => $conditions,
            'limit' => 24,
            'order' => array(
                'Game.popular_incat_ordering' => 'ASC'
            ),
        );
        $this->set('description_for_layout', "sưu tầm hàng ngàn game flash mini 3d hay nhất và nhiều người chơi nhất.");

        $data = $this->paginate('Game');

        $this->set('total', $this->params['paging']['Game']['count']);
        $this->set('data', $data);
        $this->set('thumbnail_thumb_url_path', Configure::read('game_flash_thumbnail_url_path'));
        $this->set('start_page', $page);
        $this->set('game3d_css', ' class="active"');
    }

    function popular() {
        $this->set('title_for_layout', 'Game phổ biến nhất');

        $page = array_key_exists('page', $this->params['named']) ? $this->params['named']['page'] : 1;

        $list_ids = array();
        $conditions = array(
            'Game.published' => 1,
            'Game.popular' => 1
        );
        $this->paginate = array(
            'fields' => array('Game.id, Game.alias, Game.hot_icon, Game.title, Game.thumbnail, Game.folder_url, Game.play_times'),
            'conditions' => $conditions,
            'limit' => 24,
            'order' => array(
                'Game.id' => 'DESC'
            ),
        );

        $data = $this->paginate('Game');
        shuffle($data);
        $this->set('description_for_layout', "tất cả các game flash mini phổ biến nhất có mặt tại đây");

        $this->set('total', $this->params['paging']['Game']['count']);
        $this->set('data', $data);
        $this->set('thumbnail_thumb_url_path', Configure::read('game_flash_thumbnail_url_path'));
        $this->set('start_page', $page);
        $this->set('phobiennhat_css', ' class="active"');
    }

    function hottest() {
        $this->set('title_for_layout', 'Game hay nhất');

        $page = array_key_exists('page', $this->params['named']) ? $this->params['named']['page'] : 1;

        $list_ids = array();
        $conditions = array(
            'Game.published' => 1,
            'Game.hot_game' => 1
        );
        $this->paginate = array(
            'fields' => array('Game.id, Game.alias, Game.hot_icon, Game.title, Game.thumbnail, Game.folder_url, Game.play_times'),
            'conditions' => $conditions,
            'limit' => 24,
            'order' => array(
                'Game.hot_game_ordering' => 'ASC'
            ),
        );

        $data = $this->paginate('Game');
        $this->set('description_for_layout', "tất cả các game flash mini hay nhất có mặt tại đây");

        $this->set('total', $this->params['paging']['Game']['count']);
        $this->set('data', $data);
        $this->set('thumbnail_thumb_url_path', Configure::read('game_flash_thumbnail_url_path'));
        $this->set('start_page', $page);
        $this->set('gamehaynhat_css', ' class="active"');
    }

    function lastest() {
        $this->set('title_for_layout', 'Game mới nhất');

        $page = array_key_exists('page', $this->params['named']) ? $this->params['named']['page'] : 1;

        $list_ids = array();
        $conditions = array(
            'Game.published' => 1
        );
        $this->paginate = array(
            'fields' => array('Game.id, Game.alias, Game.hot_icon, Game.title, Game.thumbnail, Game.folder_url, Game.play_times'),
            'conditions' => $conditions,
            'limit' => 24,
            'order' => array(
                'Game.id' => 'DESC'
            ),
        );
        $this->set('description_for_layout', "tất cả các game flash mini mới nhất có mặt tại đây");

        $data = $this->paginate('Game');
        $this->set('total', $this->params['paging']['Game']['count']);
        $this->set('data', $data);
        $this->set('thumbnail_thumb_url_path', Configure::read('game_flash_thumbnail_url_path'));
        $this->set('start_page', $page);
        $this->set('gamemoinhat_css', ' class="active"');
    }

    function gamehotdaily() {
        $this->set('title_for_layout', 'Game hot trong ngày');

        $page = array_key_exists('page', $this->params['named']) ? $this->params['named']['page'] : 1;

        $list_ids = array();
        $conditions = array(
            'Game.published' => 1,
            'Game.hot_game_daily' => 1
        );
        $this->paginate = array(
            'fields' => array('Game.id, Game.alias, Game.hot_icon, Game.title, Game.thumbnail, Game.folder_url, Game.play_times'),
            'conditions' => $conditions,
            'limit' => 24,
            'order' => array(
                'Game.hot_game_daily_ordering' => 'ASC'
            ),
        );

        $data = $this->paginate('Game');
        shuffle($data);
        $this->set('description_for_layout', "tất cả các game flash mini hay nhất trong ngày có mặt tại đây");

        $this->set('total', $this->params['paging']['Game']['count']);
        $this->set('data', $data);
        $this->set('thumbnail_thumb_url_path', Configure::read('game_flash_thumbnail_url_path'));
        $this->set('start_page', $page);
    }

    function category($id, $slug) {

        $category_info = $this->Game->getCategoryInfo($id);
        $this->set('title_for_layout', $category_info['title']);

        $page = array_key_exists('page', $this->params['named']) ? $this->params['named']['page'] : 1;

        $list_ids = array();
        $conditions = array(
            'Game.published' => 1,
            'Game.cat_id' => $id
        );
        $this->paginate = array(
            'fields' => array('Game.id, Game.alias, Game.hot_icon, Game.title, Game.thumbnail, Game.folder_url, Game.play_times'),
            'conditions' => $conditions,
            'limit' => 24,
            'order' => array(
                'Game.id' => 'DESC'
            ),
        );

        $data = $this->paginate('Game');
        shuffle($data);
        $this->set('description_for_layout', "tập hợp tất cả game flash mini thuộc thể loại {$category_info['title']} hay nhất hiện nay, vui nhộn và miễn phí");
        $this->set('total', $this->params['paging']['Game']['count']);
        $this->set('data', $data);
        $this->set('category_info', $category_info);
        $this->set('thumbnail_thumb_url_path', Configure::read('game_flash_thumbnail_url_path'));
        $this->set('start_page', $page);
    }

    function category_populargame($id, $slug) {
        $category_info = $this->Game->getCategoryInfo($id);
        $this->set('title_for_layout', $category_info['title'] . ' nhiều người chơi nhất');

        $page = array_key_exists('page', $this->params['named']) ? $this->params['named']['page'] : 1;

        $list_ids = array();
        $conditions = array(
            'Game.published' => 1,
            'Game.popular_incat' => 1,
            'Game.cat_id' => $id
        );
        $this->paginate = array(
            'fields' => array('Game.id, Game.alias, Game.hot_icon, Game.title, Game.thumbnail, Game.folder_url, Game.play_times'),
            'conditions' => $conditions,
            'limit' => 24,
            'order' => array(
                'Game.popular_incat_ordering' => 'ASC'
            ),
        );

        $data = $this->paginate('Game');
        
        $this->set('description_for_layout', "tập hợp tất cả game flash mini thuộc thể loại {$category_info['title']} nhiều người chơi nhất hiện nay!");
        $this->set('total', $this->params['paging']['Game']['count']);
        $this->set('data', $data);
        $this->set('category_info', $this->Game->getCategoryInfo($id));
        $this->set('thumbnail_thumb_url_path', Configure::read('game_flash_thumbnail_url_path'));
        $this->set('start_page', $page);
    }

    function category_hotgame($id, $slug) {
        $category_info = $this->Game->getCategoryInfo($id);
        $this->set('title_for_layout', $category_info['title'] . ' hay nhất');

        $page = array_key_exists('page', $this->params['named']) ? $this->params['named']['page'] : 1;

        $list_ids = array();
        $conditions = array(
            'Game.published' => 1,
            'Game.hot_game_incat' => 1,
            'Game.cat_id' => $id
        );
        $this->paginate = array(
            'fields' => array('Game.id, Game.alias, Game.hot_icon, Game.title, Game.thumbnail, Game.folder_url, Game.play_times'),
            'conditions' => $conditions,
            'limit' => 24,
            'order' => array(
                'Game.hot_game_incat_ordering' => 'ASC'
            ),
        );

        $data = $this->paginate('Game');
        $this->set('description_for_layout', "game flash mini thuộc thể loại {$category_info['title']} được đánh giá hay nhất hiện nay tại Việt Nam");

        $this->set('total', $this->params['paging']['Game']['count']);
        $this->set('data', $data);
        $this->set('category_info', $this->Game->getCategoryInfo($id));
        $this->set('thumbnail_thumb_url_path', Configure::read('game_flash_thumbnail_url_path'));
        $this->set('start_page', $page);
    }

    function category_lastest($id, $slug) {
        $category_info = $this->Game->getCategoryInfo($id);
        $this->set('title_for_layout', $category_info['title'] . ' mới nhất');

        $page = array_key_exists('page', $this->params['named']) ? $this->params['named']['page'] : 1;

        $list_ids = array();
        $conditions = array(
            'Game.published' => 1,
            'Game.cat_id' => $id
        );
        $this->paginate = array(
            'fields' => array('Game.id, Game.alias, Game.hot_icon, Game.title, Game.thumbnail, Game.folder_url, Game.play_times'),
            'conditions' => $conditions,
            'limit' => 24,
            'order' => array(
                'Game.incat_ordering' => 'ASC'
            ),
        );

        $data = $this->paginate('Game');
        $this->set('description_for_layout', "game flash mini thuộc thể loại {$category_info['title']} mới nhất hiện tại, vào trải nghiệm nhé.");

        $this->set('total', $this->params['paging']['Game']['count']);
        $this->set('data', $data);
        $this->set('category_info', $this->Game->getCategoryInfo($id));
        $this->set('thumbnail_thumb_url_path', Configure::read('game_flash_thumbnail_url_path'));
        $this->set('start_page', $page);
    }

    function detail($id, $slug) {
        $this->layout = 'detail';

        $this->scripts = array('jquery-1.7.2.min.js', 'swfobject.js', 'custom_v1.24.js', 'jquery.scrollTo-1.4.0.js');
        $fields = array(
            'show_fullscreen', 'group_id', 'thumbnail', 'content_type', 'html_content', 'flash_version', 'id', 'alias', 'title', 'cat_id', 'folder_url', 'filename', 'tags_html', 'description', 'game_width', 'game_height'
        );
        $content = $this->Game->read($fields, $id);
        if (!$content) {
            $this->redirect('/errors');
        }

        $conditions = array(
            'OR'    =>  array(
                'Comment.user_created' => $this->Auth->user('id'),
                'Comment.published' => 1,
            ),
            'AND' => array(
                'Comment.game_id' => $id,
            )
        );

        $this->paginate = array(
            'fields' => array("DATE_FORMAT(Comment.created,'%d-%m-%Y %h:%h %p') AS 'Comment.created', Comment.id, Comment.fullname, Comment.content"),
            'conditions' => $conditions,
            'limit' => 12,
            'order' => array(
                'Comment.id' => 'DESC'
            ),
        );
        $data_comments = $this->paginate('Comment');
        $this->set('total', $this->params['paging']['Comment']['count']);
        $this->set('data_comments', $data_comments);

        $this->set('title_for_layout', "{$content['Game']['title']}");
        $this->set('content', $content);
        $this->set('category_info', $this->Game->getCategoryInfo($content['Game']['cat_id']));
        $this->set('otherGames', $this->Game->getOtherGame($content['Game']['cat_id'], $id));
        $this->set('flash_url_path', Configure::read('game_flash_url_path') . $content['Game']['folder_url'] . $content['Game']['filename']);
        $this->set('flash_thumbnail', Configure::read('game_flash_thumbnail_url_path') . $content['Game']['folder_url'] . $content['Game']['thumbnail']);
    }

    function category_viewall() {
        $this->set('title_for_layout', "Danh mục game");
        $cacheName = 'list-cat-info';
        $category_all = Cache::read($cacheName, 'games');
        $this->set('description_for_layout', 'tất cả thể loại game flash mini vui nhộn và đặc sắc nhất đều có tại đây');
        $this->set('data', $category_all);
    }

    function service_get_populargameincat() {
        $this->layout = null;
        $catid = ($this->params['named']['catid']);
        $data = $this->Game->get_populargame_incat($catid);
        if (!$data) {
            echo 0;
            exit;
        } else {
            $this->set('game_data', $data);
            $this->set('cat_id', $catid);
        }
    }

    function service_get_hotgameincat() {
        $this->layout = null;
        $catid = ($this->params['named']['catid']);
        $data = $this->Game->get_hotgame_incat($catid);
        if (!$data) {
            echo 0;
            exit;
        } else {
            $this->set('game_data', $data);
            $this->set('cat_id', $catid);
        }
    }

    function service_get_lastestgameincat() {
        $this->layout = null;
        $catid = ($this->params['named']['catid']);
        $data = $this->Game->get_lastestgame_incat($catid);
        if (!$data) {
            echo 0;
            exit;
        } else {
            $this->set('game_data', $data);
            $this->set('cat_id', $catid);
        }
    }

}

?>
